/*
 *
 */
package org.jiscinvolve.astrodabis.barberry.common.data;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.junit.Test;
import org.junit.After;
import org.junit.Before;
import static org.junit.Assert.*;

import org.springframework.transaction.annotation.Transactional;
import org.springframework.test.context.transaction.TransactionConfiguration;

import org.jiscinvolve.astrodabis.barberry.common.core.Job;
import org.jiscinvolve.astrodabis.barberry.common.core.Tag;

/*
 *
 */
public class JoinTagTestCase
extends JoinTestBase
    {

    protected Tag tag ;
    protected Job job ;
    protected TapJoin join ;

    @Before
    public void before()
        {
        super.before();
        log.debug("JoinTagTestCase.before()");

        tag = account().tags().create(
            unique(
                "test-tag"
                )
            );

        //
        // Create the job.
        job = account().jobs().create(
            new Job.Bean(
                "job-name",
                "job-text",
                columnone,
                columntwo           
                )
            );

        join = rowone.joins().create(
            rowtwo
            );

        }

    @Test
    public void checkCreated()
        {
        compare(
            join.tags().create(
                job,
                tag
                ),
            join,
            tag
            );
        }    

    @Test
    public void checkCreatedValueIsNull()
        {
        assertNull(
            join.tags().create(
                job,
                tag
                ).value()
            );
        }    

    @Test
    public void checkCreatedValueNotNull()
        {
        assertNotNull(
            join.tags().create(
                job,
                tag,
                new Double(
                    0.5
                    )
                ).value()
            );
        }    

    @Test
    public void checkCreatedValueEquals()
        {
        assertEquals(
            new Double(
                0.5
                ),
            join.tags().create(
                job,
                tag,
                new Double(
                    0.5
                    )
                ).value()
            );
        }    

    @Test
    public void checkSelectFromRow()
        {
        assertNotNull(
            join.tags().select()
            );
        assertNotNull(
            join.tags().select().iterator()
            );
        assertFalse(
            join.tags().select().iterator().hasNext()
            );
        assertNotNull(
            join.tags().create(
                job,
                tag
                )
            );
        assertNotNull(
            join.tags().select()
            );
        assertNotNull(
            join.tags().select().iterator()
            );
        assertTrue(
            join.tags().select().iterator().hasNext()
            );
        compare(
            join.tags().select().iterator().next(),
            join,
            tag
            );
        }    

    @Test
    public void checkSelectFromJoinCount()
        {
        for (int count = 1 ; count <= 100 ; count++)
            {
            join.tags().create(
                job,
                tag
                );
            assertEquals(
                count,
                count(
                    join.tags().select()
                    )
                );
            }    
        }    

    @Test
    public void checkSelectFromJoinByTag()
        {
        assertNotNull(
            join.tags().select(
                tag
                )
            );
        assertNotNull(
            join.tags().select(
                tag
                ).iterator()
            );
        assertFalse(
            join.tags().select(
                tag
                ).iterator().hasNext()
            );
        assertNotNull(
            join.tags().create(
                job,
                tag
                )
            );
        assertNotNull(
            join.tags().select(
                tag
                )
            );
        assertNotNull(
            join.tags().select(
                tag
                ).iterator()
            );
        assertTrue(
            join.tags().select(
                tag
                ).iterator().hasNext()
            );
        compare(
            join.tags().select(
                tag
                ).iterator().next(),
            join,
            tag
            );
        }    

    @Test
    public void checkSelectByTagCount()
        {
        Tag frog = account().tags().create(
            "test-tag-frog"
            );
        Tag toad = account().tags().create(
            "test-tag-toad"
            );

        int frogs = 0 ;
        int toads = 0 ;

        for (int count = 0 ; count < 10 ; count++)
            {
            join.tags().create(
                job,
                frog,
                new Double(
                    count
                    )
                );
            frogs++;

            if ((count % 2) == 0)
                {
                join.tags().create(
                    job,
                    toad,
                    new Double(
                        count
                        )
                    );
                toads++;
                }

            assertEquals(
                (frogs + toads),
                count(
                    join.tags().select()
                    )
                );

            assertEquals(
                frogs,
                count(
                    join.tags().select(
                        frog
                        )
                    )
                );

            assertEquals(
                toads,
                count(
                    join.tags().select(
                        toad
                        )
                    )
                );
            }    
        }    
    }

